const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    personalLabelListIndex:-1,
    labelList:'',
    labelIdList:[],
    labelListStatus:[],
    type:''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.labelList();
    if (options.type){
      this.setData({
        type: options.type
      })
    }
  },
  // 标签列表
  labelList:function(){
    var _this = this;
    var { labelIdList } = this.data
    wx.showLoading({
      title: '加载中',
    })
    wx.request({
      url: app.globalData.baseUrl + 'user/userTaskCate',
      data: {
        token:app.globalData.token
      },
      success(res) {
        wx.hideLoading()
        console.log(res)
        var labelListStatus = res.data.data
        for (var i = 0; i < labelListStatus.length;i++){
          for (var j = 0; j < labelListStatus[i].children_task.length;j++){
            if (labelListStatus[i].children_task[j].is_status){
              labelIdList.push(labelListStatus[i].children_task[j].id)
            }
          }
        }
        if (res.data.code == 1000) {
          _this.setData({
            labelList: res.data.data,
            labelListStatus: res.data.data,
            labelIdList
          })
        } else {
          wx.showToast({
            title: res.data.message,
            icon: 'none',
            duration: 2000
          })
        }
      }
    })
  },
  // 选择标签
  personalLabelList:function(e){
    var _this = this
    var { id, index, subindex } = e.target.dataset
    var { labelIdList, labelListStatus, type } = this.data
    var data = {
      id: '',
      token: app.globalData.token,
      cancel_id: ''
    };
    console.log(labelListStatus[index].children_task[subindex].is_status)
    if (labelListStatus[index].children_task[subindex].is_status){
      data.id = '';
      data.cancel_id = id;
      labelListStatus[index].children_task[subindex].is_status = false
    }else{
      data.id = id;
      data.cancel_id = '';
      if (labelIdList.length >= 3) {
        wx.showToast({
          title: '最多只能添加3个标签！',
          icon: 'none',
          duration: 2000
        })
        return false
      }
      labelListStatus[index].children_task[subindex].is_status = true
    }
    wx.showLoading({
      title: '加载中',
    })
    wx.request({
      url: app.globalData.baseUrl + 'user/skillSave',
      data: data,
      method: "POST",
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      success(res) {
        wx.hideLoading()
        console.log(res)
        if (res.data.code == 1000) {
          labelIdList = [];
          var labelTitleList = [];
          for (var i = 0; i < labelListStatus.length; i++) {
            for (var j = 0; j < labelListStatus[i].children_task.length; j++) {
              if (labelListStatus[i].children_task[j].is_status){
                labelIdList.push(labelListStatus[i].children_task[j].id)
                if (type == 1){
                  labelTitleList.push(labelListStatus[i].children_task[j].name)
                }
              }
            }
          }
          if (type == 1){
            let pages = getCurrentPages();
            let prevPage = pages[pages.length - 2];
            prevPage.setData({
              taskid: labelIdList,
              tasktitle: labelTitleList
            })
          }
          _this.setData({
            labelListStatus,
            labelIdList
          })
        } else {
          wx.showToast({
            title: res.data.message,
            icon: 'none',
            duration: 2000
          })
        }
      }
    })
  },
  // 保存
  labelSave:function(){
    wx.navigateBack({
      delta: 1
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})